package com.hengtongxing.hejiayun.homepage;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.bigkoo.pickerview.view.TimePickerView;
import com.hengtongxing.hejiayun.R;
import com.hengtongxing.hejiayun.base.BaseActivity;
import com.hengtongxing.hejiayun.base.DataCallBack;
import com.hengtongxing.hejiayun.bean.AddPayFeeBean;
import com.hengtongxing.hejiayun.bean.BaseResponseBean;
import com.hengtongxing.hejiayun.dialog.HouseDialog;
import com.hengtongxing.hejiayun.event.OrderRefushEvent;
import com.hengtongxing.hejiayun.homepage.model.HomePageModelImpl;
import com.hengtongxing.hejiayun.homepage.model.IHomePageModel;
import com.hengtongxing.hejiayun.utils.DateTimeUtil;
import com.hengtongxing.hejiayun.utils.NumberFormatUtils;
import com.hengtongxing.hejiayun.utils.ToastUtil;
import com.hengtongxing.hejiayun.widget.AppTitleBar;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AddPayFeeActivity extends BaseActivity {

    @BindView(R.id.title_bar)
    AppTitleBar titleBar;
    @BindView(R.id.tv_community)
    TextView tvCommunity;
    @BindView(R.id.ll_community)
    LinearLayout llCommunity;
    @BindView(R.id.tv_month)
    TextView tvMonth;
    @BindView(R.id.ll_month)
    LinearLayout llMonth;
    @BindView(R.id.tv_commit)
    TextView tvCommit;
    private String house_id;
    private String house_name;
    private IHomePageModel homePageModel;
    private ArrayList<String> optionsItems = new ArrayList<>();
    private String month_number = "1";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_pay_fee);
        ButterKnife.bind(this);
        initView();
    }

    public void initView() {
        house_id = getIntent().getStringExtra("house_id");
        house_name = getIntent().getStringExtra("house_name");
        homePageModel = new HomePageModelImpl();

        tvCommunity.setText(house_name);

        optionsItems.add("1个月");
        optionsItems.add("3个月");
        optionsItems.add("6个月");
        optionsItems.add("9个月");
        optionsItems.add("12个月");
    }

    @OnClick({R.id.ll_month, R.id.tv_commit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_month:
                showGsInfo();
                break;
            case R.id.tv_commit:
                if (TextUtils.isEmpty(tvMonth.getText().toString())) {
                    ToastUtil.showShort("请选择缴费月份");
                    return;
                }
                requestFwPropertyCostsAdd();
                break;
        }
    }

    public void requestFwPropertyCostsAdd() {
        homePageModel.requestFwPropertyCostsAdd(house_id, month_number, new DataCallBack<AddPayFeeBean>() {
            @Override
            public void onSuccessful(AddPayFeeBean bean) {
                String totalPrice = NumberFormatUtils.formatDouble(Double.parseDouble(bean.getData().getMoney()) / 100);
                Intent intent = new Intent(mActivity, PropertyPayOrderActivity.class);
                intent.putExtra("order_no", bean.getData().getId() + "");
                intent.putExtra("totalPrice", totalPrice);
                startActivity(intent);
                finish();
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }


    private void showGsInfo() {// 弹出选择器

        OptionsPickerView pvOptions = new OptionsPickerBuilder(mActivity, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                if (options1 == 0) {
                    month_number = "1";
                }
                if (options1 == 1) {
                    month_number = "3";
                }
                if (options1 == 2) {
                    month_number = "6";
                }
                if (options1 == 3) {
                    month_number = "9";
                }
                if (options1 == 4) {
                    month_number = "12";
                }
                tvMonth.setText(optionsItems.get(options1));
            }
        }).setTitleText("")
                .setTitleSize(16)
                .setSubmitColor(getResources().getColor(R.color.text_color_2))//确定按钮文字颜色
                .setSubCalSize(16)
                .setCancelColor(getResources().getColor(R.color.text_color_2))//取消按钮文字颜色
                .setTitleBgColor(getResources().getColor(R.color.white))
                .setDividerColor(getResources().getColor(R.color.line_color_gray))
                .setTextColorCenter(getResources().getColor(R.color.text_color_2))
                .setContentTextSize(16)
                .setLineSpacingMultiplier(2)
                .isDialog(true)
                .build();


        pvOptions.setPicker(optionsItems);


        Dialog mDialog = pvOptions.getDialog();
        if (mDialog != null) {
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    Gravity.BOTTOM);

            params.leftMargin = 0;
            params.rightMargin = 0;
            pvOptions.getDialogContainerLayout().setLayoutParams(params);

            Window window = mDialog.getWindow();
            if (window != null) {
                window.setWindowAnimations(R.style.picker_view_slide_anim);//修改动画样式
                window.getDecorView().setPadding(0, 0, 0, 0);
                WindowManager.LayoutParams lp = window.getAttributes();
                lp.width = WindowManager.LayoutParams.MATCH_PARENT;
                lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
                lp.gravity = Gravity.BOTTOM;
            }
        }
        pvOptions.show();

    }

}
